<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title></title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/common.css"/>
		<link rel="stylesheet" type="text/css" href="../css/mui.picker.min.css"/>
		<style type="text/css">
			#box1 {
				margin-top: 60px;
				width: 90%;
				margin-left: 5%;
			}
			
			#box1 textarea {
				border-radius: 5px;
			}
			
			#box2 {
				margin-left: 5%;
			}
			
			#box2 h4 {
				margin-bottom: 5px;
				padding-bottom: 5px;
				border-bottom: 1px dashed #CCCACA;
			}
			
			.box3 {
				width: 90%;
				height: 5%;
				margin-left: 5%;
				background-color: #fff;
				border: 1px solid #ddd;
				border-radius: 5px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-back mui-icon-left-nav mui-pull-left"></a>
			<a class="mui-pull-right" id="alert_notice">发布</a>
			<h1 class="mui-title">发布通告</h1>
		</header>
		<div class="mui-content">
			<div id="box1">
				<h5 class="min-title">通告内容</h5>
				<textarea id="mycontent" name="" rows="10" cols=""></textarea>
			</div>
			<div id="box2">
				<h5 class="min-title">档期</h5>
				<p>特定日期不接受预约邀请</p>
			</div>
			<div class="mui-input-row">
				<label>档期开始</label>
				<input type="text" id="beginTime" class="mui-input-clear mui-input timePicker start" placeholder="请选择" readonly>
			</div>
			<div class="mui-input-row">
				<label>档期结束</label>
				<input type="text" id="endTime" class="mui-input-clear mui-input timePicker end" placeholder="请选择" readonly>
			</div>
		</div>
		<!--<div class="mui-input-row mui-checkbox">
			<label>所有时间段不接受邀请</label>
			<input name="checkbox1" value="Item 3" type="checkbox">
		</div>-->

		<script src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/jquery.min.js" ></script>
		<script src="../js/common.js"></script>
		<script src="../js/user.js" type="text/javascript" charset="utf-8"></script>
		<script>
			mui.plusReady(function(){
				//获取传递过来的数据
				var timearr = plus.webview.currentWebview().timearr;
				console.log(timearr[0]);
				var timearrlenth = timearr.length;
			
				var noticeInfo = {};
				mui('.mui-input-row').on('tap', '.timePicker', function() {
					var $this = $(this);//当前点击
					var $brother = $(this).parent('.mui-input-row').siblings('.mui-input-row').find('input.timePicker');//另一个
					var dDate = new Date();
					var minDate = new Date();
					minDate.setFullYear(dDate.getFullYear(), dDate.getMonth(), dDate.getDate());
					var maxDate = new Date();
					maxDate.setFullYear(dDate.getFullYear() + 1, dDate.getMonth(), dDate.getDate());
					plus.nativeUI.pickDate(function(e) {//添加日期
						var pickDate = e.date;
						var pd = pickDate.getFullYear()+"."+(pickDate.getMonth()+1)+"."+pickDate.getDate();
						console.log(pickDate.getTime());
						var dTime = new Date();
						noticeInfo[$this[0].id] = pickDate.getTime();
						$this.val(pd.toLocaleString());//显示到页面上
						if($this.hasClass('start')) {
							if(noticeInfo[$brother[0].id] && $brother[0].value) {
								if(noticeInfo[$brother[0].id] < noticeInfo[$this[0].id]) {
									mui.toast('开始时间不得大于结束时间！');
									$this.val('');
								}
							}
							//开始时间不能在已有时间段内
							for(var i = 0; i < timearrlenth ; i++){
								if(timearr[i][0] < noticeInfo[$this[0].id] && noticeInfo[$this[0].id] > timearr[i][1]){
									mui.toast('开始时间不能在已选之内');
								}
							}
						} else {
							if(noticeInfo[$brother[0].id] && $brother[0].value) {
								if(noticeInfo[$this[0].id] < noticeInfo[$brother[0].id]) {
									mui.toast('结束时间不得小于开始时间！');
									$this.val('');
								}
							}
							//结束时间不能在已有时间段内
							for(var i = 0; i < timearrlenth ; i++){
								if(timearr[i][0] < noticeInfo[$this[0].id] && noticeInfo[$this[0].id] > timearr[i][1]){
									mui.toast('结束时间不能在已选之内');
								}
							}
						}
					}, function(e) {
						$this.val('');
					}, {
						title: $this.prev('label').text() + '日期',
						date: dDate,
						minDate: minDate,
						maxDate: maxDate
					});
				});
			
				//发布
				mui('header').on( 'tap', '#alert_notice', function(){
					if(!$("#beginTime").val() || !$("#endTime").val() || !$("#mycontent").val()){
						mui.toast('信息填写不完整');
					}else {
						//获取页面数据
						//获取当前时间
						var now = new Date();
						var myreleaseTime = now.getTime();//发布时间(时间戳)
						console.log(myreleaseTime);
						noticeInfo.releaseTime = myreleaseTime;
						var mycontent = document.getElementById("mycontent").value;//通告详情
						console.log(mycontent);
						noticeInfo.content = mycontent;
						console.log(JSON.stringify(noticeInfo));
						//上传数据
						mui.plusReady(function() {
							Common.ajax({
								url : baseURL + 'mySelf/newAnnounce.json',
								type : 'POST',
								contentType : "application/json; charset=utf-8",
								data : JSON.stringify(noticeInfo)
							}, function(data) {
								console.log(JSON.stringify(data));
								if(data.code == 1) {
									mui.toast('发布成功');
									var parentPage = plus.webview.currentWebview().opener();
									mui.fire(parentPage, 'reload');
									mui.back();
								} else {
									mui.toast(data.msg);
								}
							}, function(xhr) {
								mui.toast('网络异常，请稍后再试');
								console.error('failed - ' + xhr.status + ' : ' + xhr.statusText);
							})
						});
					}
				});
			});
		</script>
	</body>

</html>